package com.example.test06thread.util;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author EDZ
 */
@Configuration
@EnableAsync
@Data
public class ThreadPoolConfig {

    private final int corePoolSize = 5;
    private final int maxPoolSize = 10;
    private final int queueCapacity = 10;
    private final int keepAliveSeconds = 10;
    private final String threadNamePrefix = "qeqwe";

    @Bean("abc")
    public ThreadPoolTaskExecutor asyncServiceExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        //核心线程数
        threadPoolTaskExecutor.setCorePoolSize(corePoolSize);
        threadPoolTaskExecutor.setAllowCoreThreadTimeOut(true);
        //最大线程数
        threadPoolTaskExecutor.setMaxPoolSize(maxPoolSize);
        //配置队列大小
        threadPoolTaskExecutor.setQueueCapacity(queueCapacity);
        //空闲线程存活时间
        threadPoolTaskExecutor.setKeepAliveSeconds(keepAliveSeconds);
        //配置线程池前缀
        threadPoolTaskExecutor.setThreadNamePrefix(threadNamePrefix);
        //拒绝策略
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }

}
